<template>
  <div>
    <!---搜索表单-->
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="12">
            <el-form-item label="关键字">
              <el-input
                v-model="form.fname"
                style="width: 100%"
                placeholder="用户名"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间">
              <el-date-picker
                v-model="createTimes"
                type="datetimerange"
                range-separator="To"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                format="YYYY-MM-DD HH:mm:ss"
                value-format="YYYY-MM-DD HH:mm:ss"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序">
              <el-select v-model="lrate">
                <el-option value="风险" />
                <el-option value="月利率" />
                <el-option value="年利率" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display:flex">
          <el-button type="primary" size="small" @click="searchSysUser">
            搜索
          </el-button>
          <el-button size="small" @click="resetData">重置</el-button>
        </el-row>
      </el-form>
    </div>

    <el-dialog v-model="dialogVisible" title="添加或修改" width="40%">
      <el-form label-width="120px">
        <el-form-item v-show="false">
          <el-input v-model="sysUser.fid" />
        </el-form-item>
        <el-form-item label="名称">
          <el-input v-model="sysUser.fname" />
        </el-form-item>
        <el-form-item label="风险等级">
          <el-select v-model="sysUser.risk">
            <el-option value="0">高</el-option>
            <el-option value="1">低</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="月利率">
          <el-input v-model="sysUser.mouthrate" />
        </el-form-item>
        <el-form-item label="年利率">
          <el-input v-model="sysUser.yearrate" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input
            v-model="sysUser.description"
            style="width: 240px"
            :autosize="{ minRows: 2, maxRows: 4 }"
            type="textarea"
            placeholder="Please input"
          />
        </el-form-item>
        <el-form-item label="日利率">
          <el-input v-model="sysUser.rate" />
        </el-form-item>
        <el-form-item label="期限">
          <el-input v-model="sysUser.term" />
        </el-form-item>
        <el-form-item label="规则">
          <el-select v-model="sysUser.rules">
            <el-option value="1元起购"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库存">
          <el-input-number v-model="sysUser.share" />
        </el-form-item>
        <el-form-item label="logo">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:9010/admin/system/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img
              v-if="sysUser.remark"
              :src="sysUser.remark"
              class="avatar"
              style="width: 30px"
            />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!--添加按钮-->
    <div class="tools-div">
      <el-button type="success" size="small" @click="addShow">添 加</el-button>
    </div>
    <!---数据表格-->
    <el-table :data="list" style="width: 100%">
      <el-table-column prop="fname" label="理财名称" />
      <el-table-column prop="remark" label="logo" #default="scope">
        <img :src="scope.row.remark" width="30" />
      </el-table-column>
      <el-table-column prop="risk" label="风险等级" #default="scope">
        <el-tag v-if="scope.row.risk === 0" type="danger">高</el-tag>
        <el-tag v-else-if="scope.row.risk === 1" type="success">低</el-tag>
      </el-table-column>
      <el-table-column label="操作" align="center" width="280" #default="scope">
        <el-button type="primary" size="small" @click="editShow(scope.row)">
          修改
        </el-button>
        <el-button type="warning" size="small" @click="deleteById(scope.row)">
          删除
        </el-button>
        <el-button type="danger" size="small" @click="updateById(scope.row)">
          {{ scope.row.status == 0 ? '下架' : '上架' }}
        </el-button>
      </el-table-column>
    </el-table>

    <!--分页条-->
    <el-pagination
      v-model:current-page="pageParamsList.page"
      v-model:page-size="pageParamsList.limit"
      :page-sizes="[2, 20, 50, 100]"
      @size-change="fetchSysUser"
      @current-change="fetchSysUser"
      layout="total, sizes, prev, pager, next"
      :total="total"
    />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'

import { ElMessage } from 'element-plus'
import { ElMessageBox } from 'element-plus'

import { useApp } from '@/pinia/modules/app'

import {
  getDeleteFunds,
  getFunds,
  getFundsInfo,
  getPostFunds,
  getUpdateState,
} from '@/api/fund'

const childBorder = ref(false)

const lrate = ref('')

//上架下架
const updateById = row => {
  const { code } = getUpdateState(row.fid)
  ElMessage.success('操作成功')
  fetchSysUser()
}

//重置
const resetData = () => {
  //from清空
  form.value.fname = ''
  form.value.createTimeBegin = ''
  form.value.createTimeEnd = ''
  lrate.value = ''
  fetchSysUser(
    pageParamsUsers.value.page,
    pageParamsUsers.value.limit,
    form.value
  )
}

// const headers = {
//   token: useApp().authorization.token, // 从pinia中获取token，在进行文件上传的时候将token设置到请求头中
// }

const dialogVisible = ref(false)

//搜索方法
const searchSysUser = () => {
  if (lrate.value === '风险') {
    form.value.risk = 1
  }
  if (lrate.value === '年利率') {
    form.value.yearrate = 0.02
  }
  if (lrate.value === '月利率') {
    form.value.mouthrate = 0.02
  }

  fetchSysUser(
    pageParamsUsers.value.page,
    pageParamsUsers.value.limit,
    form.value
  )
}
const sysUsers = {
  fid: '',
  fname: '',
  remark: '',
  risk: '',
  rate: '',
  term: '',
  tread: '',
  description: '',
  rules: '',
  share: '',
  mouthrate: '',
  yearrate: '',
}
const sysUser = ref(sysUsers)

const submit = async () => {
  const { code } = await getPostFunds(sysUser.value)
  if (code === 200) {
    dialogVisible.value = false
    sysUser.value = sysUsers
    ElMessage.success('操作成功')
    fetchSysUser()
  }
}

//  图像上传成功以后的事件处理函数
const handleAvatarSuccess = async (response, uploadFile) => {
  sysUsers.remark = response.data
}

const addShow = async () => {
  sysUser.value = ref('')
  dialogVisible.value = true
}

const queryDto = {
  lname: '',
  createTimeBegin: '',
  createTimeEnd: '',
  risk: '',
  mouthrate: '',
  yearrate: '',
}
const form = ref(queryDto)

const editShow = async row => {
  sysUser.value = row

  const { data } = await getFundsInfo(row.fid)
  sysUsers.term = data.term
  sysUsers.rate = data.rate
  sysUsers.share = data.share
  sysUsers.description = data.description
  sysUsers.rules = data.rules
  sysUsers.fid = row.fid
  sysUsers.fname = row.fname
  sysUsers.risk = row.risk
  sysUsers.remark = row.remark
  sysUsers.mouthrate = row.mouthrate
  sysUsers.yearrate = row.yearrate
  sysUser.value = sysUsers
  dialogVisible.value = true
}
//开始和结束时间数据模型
const createTimes = ref([])

const list = ref([])
const fetchSysUser = async () => {
  //获取开始和结束时间
  if (createTimes.value.length == 2) {
    form.value.createTimeBegin = createTimes.value[0]
    form.value.createTimeEnd = createTimes.value[1]
  }
  const { data } = await getFunds(
    pageParamsUsers.value.page,
    pageParamsUsers.value.limit,
    form.value
  )
  list.value = data.records
  total.value = data.total
}
onMounted(fetchSysUser)
//分页数据
const pageParamsList = {
  page: 1,
  limit: 10,
}
const total = ref(0)
const pageParamsUsers = ref(pageParamsList)

// 删除数据
const deleteById = row => {
  ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    const { code } = await getDeleteFunds(row.fid)
    if (code === 200) {
      ElMessage.success('删除成功')
      pageParamsUsers.value.page = 1
      fetchSysUser()
    }
  })
}
</script>

<style scoped>
.demo-fit {
  display: flex;
  text-align: center;
  justify-content: space-between;
}
.demo-fit .block {
  flex: 1;
  display: flex;
  flex-direction: column;
  flex-grow: 0;
}

.demo-fit .title {
  margin-bottom: 10px;
  font-size: 14px;
  color: var(--el-text-color-secondary);
}
</style>
